home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 February: Tool Chest / Dev.CD Feb 94.toast / Tool Chest / Networking & Communications / Network Watch (DMZ) 1.0 / dMZLists.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-17  |  12.3 KB  |  545 lines  |  [TEXT/KAHL]

  1. /*
  2. #-------------------------------------------------------------------------------------------
  3. #
  4. #    Program:    < DMZ 1.0 >
  5. #    File:        < dmzLists.c >
  6. #    
  7. #    by pvh
  8. #    of <Apple Macintosh Developer Technical Support - or wheverever>
  9. #
  10. #    Copyright © 1990 Apple Computer, Inc.
  11. #    All rights reserved.
  12. #    
  13. #-------------------------------------------------------------------------------------------
  14. */
  15.  
  16. #ifndef THINK_C
  17. #include    <types.h>
  18. #include    <quickdraw.h>
  19. #include    <toolutils.h>
  20. #include    <fonts.h>
  21. #include    <events.h>
  22. #include    <windows.h>
  23. #include    <dialogs.h>
  24. #include    <menus.h>
  25. #include    <desk.h>
  26. #include    <textedit.h>
  27. #include    <scrap.h>
  28. #include    <segload.h>
  29. #include    <osevents.h>
  30. #include    <files.h>
  31. #include    <devices.h>
  32. #include    <memory.h>
  33. #include    <appletalk.h>
  34. #include    <lists.h>
  35. #include    <SysEqu.h>
  36. #include    <Script.h>
  37. #include    <Packages.h>
  38. #endif
  39.  
  40. #include    "dmz.h"
  41.  
  42. /* 
  43.  *    External globals we use in this dude.
  44.  */
  45. extern     DialogPtr             gMyDialog;
  46. extern     long                    gSortMode;
  47. extern  EventRecord        gMyEvent;
  48.  
  49. /* 
  50.  *    Globals we use here
  51.  */
  52. ListHandle     gZonesList, gObjectTypeList;
  53. char             gNameGlob[33];
  54.  
  55.  
  56. /* 
  57.  *    This is a simple routine that invalidates the passed in dialog item's rectangle 
  58.  *    so that we get a cleaner refresh of only the items that need updating.
  59.  */
  60. void invalidateItem(short whichItem)
  61. {
  62.     Rect         r;
  63.     short         kind;
  64.     Handle         h;
  65.     GrafPtr     savedPort;
  66.     
  67.     GetPort(&savedPort);
  68.     SetPort(gMyDialog);
  69.     GetDItem(gMyDialog, whichItem, &kind, &h, &r);
  70.     InvalRect(&r);
  71.     BeginUpdate(gMyDialog);
  72.     UpdtDialog(gMyDialog, gMyDialog->visRgn);
  73.     EndUpdate(gMyDialog);
  74.     SetPort(savedPort);
  75. }
  76.     
  77.     
  78. void DisposOfMyLists()
  79. {
  80.     LDispose(gZonesList);
  81.     LDispose(gObjectTypeList);
  82. }
  83.     
  84. void OpenZonesList()
  85. {
  86.     Rect         dBounds, rView;
  87.     Point         cSize;
  88.     GrafPtr     tp;
  89.     short        kind;
  90.     Handle        h;
  91.     
  92.     GetPort(&tp);
  93.     SetPort(gMyDialog);
  94.     GetDItem(gMyDialog, 1, &kind, &h, &rView);
  95.  
  96.     rView.top += 1;
  97.     rView.bottom -= 1;
  98.     rView.right -= 16;
  99.  
  100.     cSize.v = 0;
  101.     cSize.h = 0;
  102.  
  103.     SetRect(&dBounds, 0, 0, 1, 0); 
  104.     gZonesList = LNew(&rView, &dBounds, cSize, 0, gMyDialog, true, false, false, true);
  105.     (**gZonesList).selFlags = lUseSense+lOnlyOne;
  106.     SetPort(tp);
  107. }
  108.  
  109. void tellUserNoZones()
  110. {
  111.     Cell             theCell;
  112.     short         ignore, i;
  113.     GrafPtr     tp;
  114.     
  115.     GetPort(&tp);
  116.     SetPort(gMyDialog);
  117.  
  118.     LDoDraw(false, gZonesList);
  119.  
  120.     i = 0;
  121.  
  122.     theCell.v = i;
  123.     theCell.h = 0;
  124.  
  125.     ignore = LAddRow(1, i, gZonesList);    
  126.  
  127.     LSetCell("No zones <*>.", PStrLen("\pNo zones <*>."), theCell, gZonesList);    
  128.  
  129.     LDoDraw(true, gZonesList);
  130.     InvalRect(&(**gZonesList).rView);
  131.  
  132.     SetPort(tp);
  133. }
  134.     
  135.  
  136. /* 
  137.  *    compare routine for qsort().  it uses a global selector for the different 'fields' inthe list
  138.  *    they are actually offsets to the data in each cell.
  139.  *    OK...so it's really gross.
  140.  */
  141. int myCompare(char *aStr, char *bStr)
  142. {
  143.     long    sortPtr;
  144.     
  145.     switch(gSortMode) {
  146.         case sortOnZoneName:
  147.              sortPtr = myNetworkEntityObject;
  148.              break;
  149.         case sortOnType:
  150.              sortPtr = myNetworkEntityType;
  151.              break;
  152.         case sortOnNet:
  153.              sortPtr = myNetworkEntityNet;
  154.              break;
  155.         case sortOnNode:
  156.              sortPtr = myNetworkEntityNode;
  157.              break;
  158.         case sortOnSocket:
  159.              sortPtr = myNetworkEntitySocket;
  160.              break;
  161.         }
  162.     return(IUCompString((void *)(aStr + sortPtr), (void *)(bStr + sortPtr)));
  163.     /*return(IUCompString((char *)aStr + sortPtr, (char *)bStr + sortPtr));*/
  164. }
  165.  
  166. /* 
  167.   *    call qsort() 
  168.   */
  169. void letsSort(Ptr theBuffPtr, short numZonesGot, short number)
  170. {        
  171.     SetPort(gMyDialog);
  172.     
  173.     /* tell them we're sorting */
  174.     ParamText("\p", "\p", "\p", "\psorting...");        
  175.     invalidateItem(kProgressID);
  176.         
  177.     /* do the sort */
  178.     qsort(theBuffPtr, (unsigned long)numZonesGot, (unsigned long)number, myCompare);
  179.     
  180.     /* erase the info text */
  181.     ParamText("\p", "\p", "\p", "\p");        
  182.     invalidateItem(kProgressID);
  183. }
  184.  
  185.  
  186. /* 
  187.  *    set up the zone list 
  188.  */
  189. void SetZoneCells(Ptr bufferPtr, short NumZonesGot)
  190. {
  191.     Cell         theCell;
  192.     short         ignore, i;
  193.     long         bufferIndex;
  194.     GrafPtr     tp;
  195.     Str32        myZone, zoneString;
  196.     
  197.     MYVBLSPININSTALL();
  198.  
  199.     myZone[0] = 0;
  200.     getMyZone(&myZone);
  201.     
  202.     GetPort(&tp);
  203.     SetPort(gMyDialog);
  204.  
  205.     /* added 5/5/89 pvh */
  206.     gSortMode = sortOnZoneName;
  207.     letsSort(bufferPtr, NumZonesGot, zoneNameSize);
  208.     
  209.     LDoDraw(false, gZonesList);
  210.     
  211.     bufferIndex = 0L;
  212.     i = 0;
  213.     while(i<NumZonesGot) {
  214.         ignore = LAddRow(1, i, gZonesList);    
  215.         theCell.v = i;
  216.         theCell.h = 0;
  217.                 
  218.         zoneString[0] = (char)(bufferPtr+bufferIndex)[0];
  219.         LSetCell((Ptr)bufferPtr+bufferIndex + 1L, zoneString[0], theCell, gZonesList);    
  220.  
  221.         /*
  222.          *    when we find our zone, select it in the list
  223.          */
  224.         zoneString[0] = (char)(bufferPtr+bufferIndex)[0];
  225.         BlockMove((Ptr)bufferPtr+bufferIndex + 1L, (Ptr)&zoneString + 1L, zoneString[0]);
  226.         if(EqualString(zoneString, myZone, false, false)) {
  227.             LSetSelect(true, theCell, gZonesList);
  228.             LAutoScroll(gZonesList);
  229.             getTypesNamesInZone(&zoneString);
  230.             }
  231.             
  232.         i += 1;
  233.         bufferIndex += zoneNameSize;
  234.         }
  235.  
  236.     LDoDraw(true, gZonesList);
  237.  
  238.     SetPort(tp);
  239.  
  240.     STOPANDREMOVESPINNINGCURSOR();
  241. }
  242.  
  243. /* 
  244.  *    handle click in the zone list box 
  245.  */
  246. void doZonesListStuff()
  247. {
  248.     Boolean     tempBool;
  249.     Cell         thisCell;
  250.     Rect         scrollRect;
  251.     GrafPtr     tp;
  252.     Str255          dataPtr;
  253.     short         dataLen;
  254.     Point        localPoint = gMyEvent.where;
  255.     
  256.     thisCell.v = 0;
  257.     thisCell.h = 0;
  258.     GetPort(&tp);
  259.     SetPort(gMyDialog);
  260.     
  261.     if(gMyDialog != FrontWindow())
  262.         SelectWindow(gMyDialog);
  263.     else {
  264.         GlobalToLocal(&localPoint);
  265.         scrollRect = (**gZonesList).rView;
  266.         scrollRect.left = (**gZonesList).rView.right;
  267.         scrollRect.right = scrollRect.left + 15;
  268.         if(PtInRect(localPoint, &scrollRect))
  269.             tempBool = LClick(localPoint, gMyEvent.modifiers, gZonesList);
  270.         else if(PtInRect(localPoint, &(**gZonesList).rView)) {
  271.             if(LClick(localPoint, gMyEvent.modifiers, gZonesList)) {    
  272.                 LGetSelect(true, &thisCell, gZonesList);
  273.                 dataLen = 255;
  274.                 LGetCell(&dataPtr[1], &dataLen, thisCell, gZonesList);
  275.                 dataPtr[0] = dataLen;
  276.                 BlockMove(&dataPtr, &gNameGlob[0], 34L);
  277.                 getTypesNamesInZone(&dataPtr);
  278.                 }
  279.             else {
  280.                 LGetSelect(true, &thisCell, gZonesList);
  281.                 dataLen = 255;
  282.                 LGetCell(&dataPtr[1], &dataLen, thisCell, gZonesList);
  283.  
  284.                 dataPtr[0] = dataLen;
  285.  
  286.                 BlockMove(&dataPtr[0], &gNameGlob[0], 34L);
  287.                 }
  288.             }
  289.         }
  290.     SetPort(tp);
  291. }
  292.  
  293. void OpenObjectTypeList()
  294. {
  295.     Rect         dBounds, rView;
  296.     Point         cSize;
  297.     GrafPtr     tp;
  298.     Handle        h;
  299.     short        kind, ignore;
  300.     
  301.     GetPort(&tp);
  302.     SetPort(gMyDialog);
  303.     GetDItem(gMyDialog, 2, &kind, &h, &rView);
  304.  
  305.     rView.top += 1;
  306.     /*
  307.     // rView.bottom -= 1;
  308.     // rView.bottom = gMyDialog->portRect.bottom-16;
  309.     */
  310.     
  311.     rView.right -= 16;
  312.  
  313.     cSize.v = 0;
  314.     cSize.h = 0;
  315.  
  316.     SetRect(&dBounds, 0, 0, 1, 0); /* two cells across by 0 down extra cell for hiding entity net address */
  317.     gObjectTypeList = LNew(&rView, &dBounds, cSize, 128, gMyDialog, true, false, false, true);
  318.     (**gObjectTypeList).selFlags = lUseSense+lOnlyOne;
  319.     
  320.     SizeControl((**gObjectTypeList).vScroll, (**(**gObjectTypeList).vScroll).contrlRect.right - 
  321.         (**(**gObjectTypeList).vScroll).contrlRect.left, (**(**gObjectTypeList).vScroll).contrlRect.bottom - 
  322.         (**(**gObjectTypeList).vScroll).contrlRect.top - 16);
  323.     
  324.     ignore = LAddColumn(1, 0, gObjectTypeList);    
  325.  
  326.     SetPort(tp);
  327. }
  328.  
  329. void doObjectDoubleClick()
  330. {
  331.     Cell                 thisCell;
  332.     short                dataLen;
  333.     long                temp;
  334.     myNetworkEntity        myEnt;
  335.     AddrBlock            address;
  336.  
  337.     thisCell.v = 0;
  338.     thisCell.h = 0;
  339.     LGetSelect(true, &thisCell, gObjectTypeList);
  340.     
  341.     /* 
  342.      *    at this point do whatever you want with the object selection.
  343.      *    send 'em lots of packets and try and kill them or whatever...
  344.      */
  345.     dataLen = sizeof(myNetworkEntity);
  346.     LGetCell((Ptr) &myEnt, &dataLen, thisCell, gObjectTypeList);
  347.  
  348.     StringToNum((void *) myEnt.net, (long *) &temp);
  349.     address.aNet = (short) temp;
  350.     StringToNum((void *) myEnt.node, (long *) &temp);
  351.     address.aNode = (short) temp;
  352.     StringToNum((void *) myEnt.socket, (long *) &temp);
  353.     address.aSocket = (short) temp;
  354.     doEcho(&myEnt);
  355. }
  356.  
  357.  
  358. void doObjectTypeListStuff()
  359. {
  360.     Boolean             ignore;
  361.     Cell                 thisCell;
  362.     Rect                 scrollRect;
  363.     GrafPtr             tp;
  364.     Point                localPoint = gMyEvent.where;
  365.     
  366.     thisCell.v = 0;
  367.     thisCell.h = 0;
  368.     GetPort(&tp);
  369.     SetPort(gMyDialog);
  370.     
  371.     if(gMyDialog != FrontWindow())
  372.         SelectWindow(gMyDialog);
  373.     else {
  374.         GlobalToLocal(&localPoint);
  375.         scrollRect = (**gObjectTypeList).rView;
  376.         scrollRect.left = (**gObjectTypeList).rView.right;
  377.         scrollRect.right = scrollRect.left + 15;
  378.         if(PtInRect(localPoint, &scrollRect))
  379.             ignore = LClick(localPoint, gMyEvent.modifiers, gObjectTypeList);
  380.         else if(PtInRect(localPoint, &(**gObjectTypeList).rView)) {
  381.             /*
  382.              *    Check for a double click.
  383.              */
  384.              if(LClick(localPoint, gMyEvent.modifiers, gObjectTypeList)) {
  385.                 doObjectDoubleClick();
  386.                 }
  387.             }
  388.         }
  389.     SetPort(tp);
  390. }
  391.  
  392. /* 
  393.  *    this routine pads strings out to various lengths.  justification to right or left can 
  394.  *    be specified.  we need this for a cleaner display as well as insuring a correct sort
  395.  *    on numeric data (i.e. the strings '1234' and ' 1234' are not equivalant, but if
  396.  *    we justify to the right, we'll always be OK.  (whoops maybe not in the case of the Japanese
  397.  *    alphabet...damn...
  398.  */
  399. void padEntry(char *entry, short length, short just)
  400. {
  401.     unsigned char     index, i;
  402.     
  403.     switch(just) {
  404.         case leftJust:
  405.             index = entry[0];    /* get length byte */
  406.             index += 1;            /* increment by 1 */
  407.         
  408.             while(index <= length) {
  409.                 entry[index] = ' ';
  410.                 index += 1;
  411.                 }    
  412.             break;
  413.         case rightJust:
  414.             index = entry[0];    /* get length byte */
  415.         
  416.             if(index<length) {
  417.                 BlockMove(&entry[1], (Ptr)&entry[1]+length-index, (long)index);
  418.                 
  419.                 i = 1;
  420.                 while(index < length) {
  421.                     entry[i] = ' ';
  422.                     i += 1;
  423.                     index += 1;
  424.                     }    
  425.                 }
  426.             break;
  427.         }
  428.     entry[0] = length;
  429. }
  430.     
  431. /* 
  432.  *    this is called when the NBPLookup has finished.  the buffer and the number of obects found
  433.  *    is added to the list.
  434.  */
  435. void SetObjectTypeCells(Ptr bufferPtr, short numDevicesGot)
  436. {
  437.     Cell                         theCell;
  438.     short                     ignore;
  439.     short                 numDevicesIndex;
  440.     GrafPtr             tp;
  441.     AddrBlock             address;
  442.     EntityName            abEntity;
  443.     char                charHolder[6];
  444.     long                g;
  445.     myNetworkEntity     myNetEnt;
  446.     Ptr                    newBuffer;
  447.     char                tempStr[10];
  448.     
  449.     /* tell the user this may take a little while... */
  450.     /*waitAWhile = GetCursor(watchCursor);
  451.     SetCursor(*waitAWhile);*/
  452.     MYVBLSPININSTALL();
  453.     
  454.     GetPort(&tp);
  455.     SetPort(gMyDialog);
  456.  
  457.     /* make room for as many objects as we need */
  458.     newBuffer = NewPtr(numDevicesGot*sizeof(myNetworkEntity));
  459.     if(newBuffer == 0L)
  460.         return;
  461.         
  462.     LDelRow(0, 0, gObjectTypeList);    /* deletes lists's cells */
  463.     
  464.     LDoDraw(false, gObjectTypeList);  /* turn drawing off */
  465.     
  466.     numDevicesIndex = 0;
  467.     
  468.     while(numDevicesIndex<numDevicesGot) {
  469.         theCell.v = numDevicesIndex;
  470.         
  471.         ignore = LAddRow(1, numDevicesIndex, gObjectTypeList);    
  472.  
  473.         NBPExtract(bufferPtr, numDevicesGot, numDevicesIndex+1, &abEntity, &address);
  474.  
  475.         /* first move address data into "hidden" cell */
  476.         theCell.h = 1;
  477.         LSetCell((Ptr) &address, 4, theCell, gObjectTypeList);
  478.         
  479.         /* now move object name & type into one cell */
  480.         theCell.h = 0;
  481.         
  482.         /* object */
  483.         BlockMove(&abEntity.objStr, &myNetEnt.object, 33L);    
  484.         if(myNetEnt.object[0] > 32)
  485.             myNetEnt.object[0] = 32;
  486.             
  487.         /* type */
  488.         BlockMove(&abEntity.typeStr, &myNetEnt.type, 33L);
  489.         if(myNetEnt.type[0] > 32)
  490.             myNetEnt.type[0] = 32;
  491.         
  492.         /* network */
  493.         g = (long)address.aNet;
  494.         g = g & 0x0000FFFF; /* mask out hiword crap */
  495.         NumToString(g, (void *) &charHolder);
  496.         padEntry((void *) &charHolder, 5, rightJust);
  497.         BlockMove(&charHolder, &myNetEnt.net, 6L);
  498.  
  499.         /* node */
  500.         NumToString((long)address.aNode, (void *) &charHolder);
  501.         padEntry((void *) &charHolder, 3, rightJust);
  502.         BlockMove(&charHolder, &myNetEnt.node, 4L);
  503.     
  504.         /* socket */
  505.         NumToString((long)address.aSocket, (void *) &charHolder);
  506.         padEntry((void *) &charHolder, 3, rightJust);
  507.         BlockMove(&charHolder, &myNetEnt.socket, 4L);
  508.                 
  509.         BlockMove((Ptr) &myNetEnt, (Ptr)newBuffer+(numDevicesIndex)*sizeof(myNetworkEntity), 
  510.             sizeof(myNetworkEntity));
  511.  
  512.         LSetCell((Ptr)&myNetEnt, sizeof(myNetworkEntity), theCell, gObjectTypeList);    
  513.  
  514.         numDevicesIndex += 1; 
  515.         }
  516.  
  517.     /* do a quicksort() on the mess */
  518.     letsSort(newBuffer, numDevicesGot, sizeof(myNetworkEntity));
  519.     
  520.     numDevicesIndex = 0;
  521.     
  522.     while(numDevicesIndex<numDevicesGot) {
  523.         theCell.v = numDevicesIndex;
  524.         theCell.h = 0;
  525.         BlockMove((Ptr)newBuffer+(numDevicesIndex)*sizeof(myNetworkEntity), (Ptr) &myNetEnt, sizeof(myNetworkEntity));
  526.         LSetCell((Ptr)&myNetEnt, sizeof(myNetworkEntity), theCell, gObjectTypeList);    
  527.         numDevicesIndex += 1;
  528.         }
  529.         
  530.     DisposPtr(newBuffer);
  531.     
  532.     NumToString(numDevicesIndex, (void *) tempStr);
  533.     ParamText(tempStr, "\p# of objects: ", "\p", "\p");
  534.     
  535.     LDoDraw(true, gObjectTypeList);
  536.     invalidateItem(8);
  537.     invalidateItem(2);
  538.  
  539.     SetPort(tp);
  540.     STOPANDREMOVESPINNINGCURSOR();
  541.     InitCursor();
  542. }
  543.  
  544.  
  545.